//1导入http模块
const http = require("http")
//2导入fs模块
const fs = require("fs")
//3导入path模块
const path = require("path")
//require()导入js文件时会立即加载js文件，但不能访问模块内部的变量 防止全局变量污染
//创建wbe服务器
const server = http.createServer()
server.on("request", function (req, res) {
    //获取客户端请求的url
    const url = req.url
    let fpath = ""
    if (url == "/") {
        fpath = path.join(__dirname, "../files/01.html")
    } else {
        //把请求的URL地址映射为具体文件的存放路径
        fpath = path.join(__dirname,"../files",url)
    }

    //读取文件
    fs.readFile(fpath, "utf8", (err, dataStr) => {
        res.end(dataStr)
    })
})
server.listen(8080)